﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PasswordProvider.ViewModel.Helpers
{
    /// <summary>
    /// Helper class to get an icon url if the site url is known.
    /// </summary>
    static class FaveIconUrlHelper
    {
        public static Uri GetFaveIconUri(Uri siteUrl, Uri siteIconUrl)
        {
            string url = GetFaveIconUrl(siteUrl, siteIconUrl);
            if(url != null)
                return new Uri(url, UriKind.Absolute);
            return null;
        }

        public static string GetFaveIconUrl(Uri siteUrl, Uri siteIconUrl)
        {
            // If there is an explicit url, return it.
            if (siteIconUrl != null)
            {
                if (siteIconUrl.IsAbsoluteUri)
                    return siteIconUrl.AbsoluteUri;
            }

            // Use the site url along with google's service to get the fave icon.
            if (siteUrl != null)
            {
                return @"http://www.google.com/s2/favicons?domain=" + siteUrl.Host;           
            }

            return null;
        }

    }
}
